package maple


abstract class Item
case class Article(desc:String,price:Double) extends Item
case class Bundle(desc:String,discount:Double,items:Item*) extends Item




object ItemTest {

  def main(args: Array[String]): Unit = {
      val res = price(Bundle("零食",15,Article("薯片",6.5),Article("香肠",10.5)))
      println(res)

      val article = Article("薯片",6.5)
      val opt = Article.unapply(article)
//      val res1 = opt.map{case (desc,price)=> println(s"desc  ${desc}  price  ${price}")}
//      println(res1)
      opt.foreach{case (desc,price)=> println(s"desc  ${desc}  price  ${price}")}
  }

  /**
    * 匹配嵌套结构,
    *
    * @param it
    * @return
    */
  def price(it:Item):Double = it match {
    case Article(_,p) => p
    case Bundle(_,discount,its @ _*) => its.map(price _).sum - discount
  }

}
